package com.algorithms.contests.srm487;

import java.util.ArrayList;
import java.util.List;

public class BunnyConverter
{
    int k=0;
    int  nFinal;
    int  zFinal;
    boolean flag=false;
	public int getMinimum(int n, int z, int start, int goal)
	{
		nFinal=n;
		zFinal=z;
		int totalCount=0;
		int subgoal=start;
		//List countList=new  ArrayList();
		while(subgoal!=-1 &&  subgoal!=goal)
		{
			
			List<Integer> possibleGoaloptions=findGoal(subgoal);
			if(possibleGoaloptions.size()==0)
			{
				return 0;
			}
			for(int subgoaloption :possibleGoaloptions)
			{
				findGoal(subgoaloption);
			}
			if(subgoal>-1)
			{
				totalCount++;
			}
			
		}
		return totalCount;
	}
	
	private List<Integer> findGoal(int start)
	{
		List<Integer> possiblePaths = new ArrayList<Integer>();
			for(int i=1;i<=nFinal;i++)
			{
				int y =(int) (i*nFinal - (start +  Math.pow(zFinal, 3)));
				if(y>0)
				{
					
					double yVal=Math.sqrt(y);
					double floor=Math.floor(yVal);
					if( (yVal -floor)==0.0)
					{
						possiblePaths.add(y);
					}
				}

			}
		return possiblePaths;
	}
	
	
	/**
	 * @param args
	 */
	public static void main(String[] args)
	{
		BunnyConverter converter = new BunnyConverter();
		System.out.println(converter.getMinimum(5, 1, 5,3));

	}

}
